package com.tzpay.tzpay.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.tzpay.tzpay.bean.FunctionInfoWrapper;

import java.util.ArrayList;

/**
 * Created by chenfeiyue on 16/4/7.
 * 图片轮播adapter
 */
public class MyBannerViewPagrAdapter extends PagerAdapter {
    private ArrayList<FunctionInfoWrapper.ItemsEntity.BannersEntity> headInfos;
    private Context context;


    private OnBannerClickListener clickListener;

    public void setResources(ArrayList<FunctionInfoWrapper.ItemsEntity.BannersEntity> headInfos) {
        this.headInfos = headInfos;
        notifyDataSetChanged();
    }

    public MyBannerViewPagrAdapter(Context context, ArrayList<FunctionInfoWrapper.ItemsEntity.BannersEntity> headInfos) {
        this.context = context;
        this.headInfos = headInfos;//构造方法，参数是我们的页卡，这样比较方便。
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);//删除页卡
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {  //这个方法用来实例化页卡

        ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        container.addView(imageView);
        final FunctionInfoWrapper.ItemsEntity.BannersEntity banner = headInfos.get(position);

        Glide.with(context)
                .load(banner.getImage()).transform(new GlideRoundTransform(context, 8))
                .into(imageView);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null) {
                    clickListener.onBannerClick(banner);
                }
            }
        });
        return imageView;
    }

    @Override
    public int getCount() {
        return headInfos.size();//返回页卡的数
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;//官方提示这样写
    }

    /**
     * banner点击事件
     *
     * @param clickListener
     */
    public void setOnBannerClickListener(OnBannerClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public interface OnBannerClickListener {
        void onBannerClick(FunctionInfoWrapper.ItemsEntity.BannersEntity banner);
    }

}